package com.aaa.service.Impl;

import com.aaa.mapper.StrudnetMapper;
import com.aaa.pojos.PackBean;
import com.aaa.pojos.Student;
import com.aaa.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StrudnetMapper strudnetMapper;

    @Override
    public PackBean<Student> query(String name, Integer degree, Integer clazzId, int page, int pageSize) {
       PageHelper.startPage(page,pageSize);

        List<Student> students=strudnetMapper.query();

        Page<Student> studentPage= (Page<Student>) students;
        return new PackBean<>((int)studentPage.getTotal(),studentPage.getResult());
    }

    @Override
    public void insertStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        strudnetMapper.insertStudent(student);
    }

    @Override
    public Student queryId(Integer id) {
        Student student=strudnetMapper.queryId(id);
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        strudnetMapper.updateStudent(student);
    }

    @Override
    public void deleteStudent(List<Integer> ids) {
        strudnetMapper.deleteStudent(ids);
    }

    @Override
    public void violation(Integer id, Integer score) {
        Student student=strudnetMapper.queryId(id);
        student.setViolationCount(student.getViolationCount()+1);
        student.setViolationScore(student.getViolationScore()+score);
        strudnetMapper.violation(student);

    }


}
